﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ARS.Common.Helper
{
    public class DateTimeHelper
    {
        /// <summary>
        /// 获取季度时间区间
        /// </summary>
        /// <param name="BeginDate"></param>
        /// <returns></returns>
        public static object[] GetNewTime(DateTime BeginDate)
        {
            string start = "", end = "", title = "";
            switch (BeginDate.Month)
            {
                case 1:
                case 2:
                case 3:
                    if (BeginDate.Month == DateTime.Now.Month)
                    {
                        start = BeginDate.ToString("yyyy-MM-dd");
                        end = DateTime.Now.ToString("yyyy") + "-03-31";
                        title = "Q1";
                    }
                    else
                    {
                        object[] obj = GetQuarter(DateTime.Now.Month);
                        start = obj[0].ToString();
                        end = obj[1].ToString();
                        title = obj[2].ToString();
                    }
                    break;
                case 4:
                case 5:
                case 6:
                    if (BeginDate.Month == DateTime.Now.Month)
                    {
                        start = BeginDate.ToString("yyyy-MM-dd");
                        end = DateTime.Now.ToString("yyyy") + "-06-30";
                        title = "Q2";
                    }
                    else
                    {
                        object[] obj = GetQuarter(DateTime.Now.Month);
                        start = obj[0].ToString();
                        end = obj[1].ToString();
                        title = obj[2].ToString();
                    }
                    break;
                case 7:
                case 8:
                case 9:
                    if (BeginDate.Month == DateTime.Now.Month)
                    {
                        start = BeginDate.ToString("yyyy-MM-dd");
                        end = DateTime.Now.ToString("yyyy") + "-09-30";
                        title = "Q3";
                    }
                    else
                    {
                        object[] obj = GetQuarter(DateTime.Now.Month);
                        start = obj[0].ToString();
                        end = obj[1].ToString();
                        title = obj[2].ToString();
                    }
                    break;
                case 10:
                case 11:
                case 12:
                    if (BeginDate.Month == DateTime.Now.Month)
                    {
                        start = BeginDate.ToString("yyyy-MM-dd");
                        end = DateTime.Now.ToString("yyyy") + "-12-31";
                        title = "Q4";
                    }
                    else
                    {
                        object[] obj = GetQuarter(DateTime.Now.Month);
                        start = obj[0].ToString();
                        end = obj[1].ToString();
                        title = obj[2].ToString();

                    }

                    break;
            }

            return new object[] { start, end, title };
        }

        /// <summary>
        /// 获取年度时间区间
        /// </summary>
        /// <param name="BeginDate"></param>
        /// <returns></returns>
        public static object[] GetNewTimeYear(DateTime BeginDate)
        {
            string start;
            string end;
            string title;
            if (BeginDate.Year == DateTime.Now.Year)
            {
                start = BeginDate.ToString("yyyy-MM-dd");
                end = DateTime.Now.ToString("yyyy") + "-12-31";
                title = BeginDate.Year.ToString();
            }
            else
            {
                start = DateTime.Now.ToString("yyyy") + "-01-01";
                end = DateTime.Now.ToString("yyyy") + "-12-31";
                title = DateTime.Now.Year.ToString();
            }

            return new object[] { start, end, title };
        }

        /// <summary>
        /// 获取季度
        /// </summary>
        /// <param name="month"></param>
        /// <returns></returns>
        public static object[] GetQuarter(int month)
        {
            string start = "", end = "", title = "";
            switch (month)
            {
                case 1:
                case 2:
                case 3:
                    start = DateTime.Now.ToString("yyyy") + "-01-01";
                    end = DateTime.Now.ToString("yyyy") + "-03-31";
                    title = "Q1";
                    break;
                case 4:
                case 5:
                case 6:
                    start = DateTime.Now.ToString("yyyy") + "-04-01";
                    end = DateTime.Now.ToString("yyyy") + "-06-30";
                    title = "Q2";
                    break;
                case 7:
                case 8:
                case 9:
                    start = DateTime.Now.ToString("yyyy") + "-07-01";
                    end = DateTime.Now.ToString("yyyy") + "-09-30";
                    title = "Q3";
                    break;
                case 10:
                case 11:
                case 12:
                    start = DateTime.Now.ToString("yyyy") + "-10-01";
                    end = DateTime.Now.ToString("yyyy") + "-12-31";
                    title = "Q4";
                    break;
            }
            return new object[] { start, end, title };
        }


        /// <summary>
        /// 通过年份和周获取时间段
        /// </summary>
        /// <param name="week">周</param>
        /// <param name="year">年</param>
        /// <returns>[0]：开始时间 [1]:结束时间</returns>
        public static DateTime[] GetDateByWeek(int week, int year, out int WeekCount)
        {
            DateTime[] TheDate = new DateTime[2];
            WeekCount = GetYearWeekCount(year);
            week -= 1;
            if (week > -1 && week <= WeekCount)
            {
                System.DateTime FristDate = DateTime.Parse(year.ToString() + "-01-01");
                int k = Convert.ToInt32(FristDate.DayOfWeek);//得到该年的第一天是周几 
                if (k == 0) { k += 1; }
                else if (k != 0) { k = 7 - k + 1; }

                FristDate = FristDate.AddDays(k);
                TheDate[0] = FristDate.AddDays(week * 7);
                TheDate[1] = FristDate.AddDays((week + 1) * 7 - 1);
            }
            else
            {
                TheDate[0] = DateTime.Now;
                TheDate[1] = DateTime.Now;
            }
            return TheDate;
        }
        /// <summary>
        /// 获取一年内有几个周(自然周，有年跨度的不算)
        /// </summary>
        /// <param name="strYear">年份</param>
        /// <returns>周数</returns>
        public static int GetYearWeekCount(int strYear)
        {
            System.DateTime StartTime = DateTime.Parse(strYear.ToString() + "-01-01");
            int k = Convert.ToInt32(StartTime.DayOfWeek);//得到该年的第一天是周几 
            if (k == 0) { k += 1; }
            else if (k != 0) { k = 7 - k + 1; }
            DateTime FristDate = StartTime;
            FristDate = FristDate.AddDays(k);
            int Days = FristDate.DayOfYear;
            Days = (StartTime.AddYears(1).AddDays(-1).DayOfYear - Days);
            int WeekCount;
            if (k == 1)
            {
                WeekCount = Days / 7 + 1;
            }
            else
            {
                WeekCount = Days / 7 + 2;
            }
            return WeekCount;
        }


        /// <summary>
        /// 获取距离1970年共计多少秒
        /// </summary>
        /// <returns></returns>
        public static long GetLongbyTime(DateTime now)
        {
            return Convert.ToInt64((now - DateTime.Parse("1970-01-01")).TotalSeconds);
        }
        /// <summary>
        /// 获取时间
        /// </summary>
        /// <param name="times"></param>
        /// <returns></returns>
        public static DateTime GetTimebyLong(long times)
        {
            DateTime date = DateTime.Parse("1970-01-01");
            return date.AddSeconds(times);
        }

        /// <summary>
        /// 获取起止时间天数
        /// </summary>
        /// <param name="dateStart"></param>
        /// <param name="dateEnd"></param>
        /// <returns></returns>
        public static int DateDiff(DateTime dateStart, DateTime dateEnd)
        {
            DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
            DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());

            TimeSpan sp = end.Subtract(start);

            return sp.Days;
        }


    }
}

